package com.yuyu.qingmangmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.Query;
import com.yuyu.qingmangmall.product.dao.SkuInfoDao;
import com.yuyu.qingmangmall.product.entity.SkuImagesEntity;
import com.yuyu.qingmangmall.product.entity.SkuInfoEntity;
import com.yuyu.qingmangmall.product.entity.SpuInfoDescEntity;
import com.yuyu.qingmangmall.product.service.*;
import com.yuyu.qingmangmall.product.vo.PricesVo;
import com.yuyu.qingmangmall.product.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntityA) {
        this.baseMapper.insert(skuInfoEntityA);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        /*
        key:
        catelogId: 225
        brandId: 0
        min: 0
        max: 0
        */
        String key = (String) params.get("key");
        if (!StringUtils.isNullOrEmpty(key)) {
            wrapper.and(wrap->{
                wrap.eq("sku_id",key).or().like("sku_name",key);
            });
        }

        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isNullOrEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            wrapper.eq("catalog_id",catelogId);
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isNullOrEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            wrapper.eq("brand_id",brandId);
        }

        String min = (String) params.get("min"); // price
        if (!StringUtils.isNullOrEmpty(min)) {
            wrapper.ge("price",min);
        }

        String max = (String) params.get("max"); // price
        if (!StringUtils.isNullOrEmpty(max) && !"0".equalsIgnoreCase(max)) {
            wrapper.le("price",max);
        }

        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params),
                wrapper);


        return new PageUtils(page);
    }


    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        List<SkuInfoEntity> skuInfos = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return skuInfos;
    }

    /**
     * 异步编排  获取商品详情信息
     * @param skuId
     * @return
     */
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            // 1.sku基本属性  pms_sku_info
            SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
            skuItemVo.setSkuInfoEntity(skuInfo);
            return skuInfo;
        }, executor);

        CompletableFuture<Void> salesAttrFuture = infoFuture.thenAcceptAsync(res -> {
            // 3.获取spu的销售属性组合
            List<SkuItemVo.SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttrs(saleAttrVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(res -> {
            // 4.获取spu的介绍  pms_spu_info_desc
            SpuInfoDescEntity infoDesc = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesp(infoDesc);
        }, executor);

        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync(res -> {
            // 5.获取spu的规格参数的信息 standard
            List<SkuItemVo.SkuItemAttrGroupVo> groupAttrs = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(),res.getCatalogId());
            skuItemVo.setGroupAttrs(groupAttrs);
        }, executor);

        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            // 2.sku的图片信息 pms_sku_images
            List<SkuImagesEntity> images = skuImagesService.getImgsBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);

        // 等待所有任务都做完
        CompletableFuture.allOf(salesAttrFuture,descFuture,baseAttrFuture,imgFuture).get();
        return skuItemVo;
    }

    @Override
    public List<PricesVo> getNewPrices(Long... skuIds) {
        SkuInfoDao baseMapper = this.baseMapper;

        return baseMapper.getNewPrices(skuIds);
    }


}